package com.example.camerademo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	// ��ʾͼƬImageView
	private ImageView photo;
	// ϵͳ��ǰʱ��
	private String pig;
	// ѡ���ϴ���ʽdialog
	private AlertDialog mDialog;
	// ͼƬ����·��
	public String SDCARD = Environment.getExternalStorageDirectory()
			+ "/Android/data/com/camera/demo/";
	// ��Ƭ���������λ��  (�ϴ�ʱ �����File�Ϳ���)
	private File filen;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		photo = (ImageView) findViewById(R.id.photo);
		photo.setOnClickListener(this);
	}

	/**
	 * ѡ��ʽdialog
	 */
	private void showPhotoDialog() {
		mDialog = new AlertDialog.Builder(MainActivity.this).create();
		mDialog.setCanceledOnTouchOutside(false);

		View view = LayoutInflater.from(MainActivity.this).inflate(
				R.layout.dialog_xuanzezhaopian, null);
		mDialog.show();
		mDialog.setContentView(view);
		mDialog.getWindow().setGravity(Gravity.BOTTOM);
		mDialog.getWindow().setLayout(
				getWindowManager().getDefaultDisplay().getWidth() - 30,
				android.view.WindowManager.LayoutParams.WRAP_CONTENT);
		Button bt = (Button) view.findViewById(R.id.dialog_quxiao);
		TextView paizhaoshangchuan = (TextView) view
				.findViewById(R.id.paizhaoshangchuan);
		TextView shoujixiangce = (TextView) view
				.findViewById(R.id.shoujixiangce);
		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mDialog.dismiss();
			}
		});
		paizhaoshangchuan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mDialog.dismiss();
				Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				String file = SDCARD;
				File f = new File(file);
				if (!f.exists()) {
					f.mkdirs();
				}
				Date date = new Date(System.currentTimeMillis());
				pig = date.getTime() + "";
				f = new File(file + pig + ".jpg");
				if (f.exists()) {
					f.delete();
				}
				try {
					f.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
				Uri uri = Uri.fromFile(f);
				Log.e("uri", uri + "");
				camera.putExtra(MediaStore.EXTRA_OUTPUT, uri);
				startActivityForResult(camera, 1);
			}
		});
		shoujixiangce.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mDialog.dismiss();
				Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT);
				intent2.setType("image/*");
				startActivityForResult(intent2, 2);
			}
		});
	}

	/**
	 * ͼƬ����
	 * 
	 * @param uri
	 */
	public void startPhotoZoom(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// �������crop=true�������ڿ�����Intent��������ʾ��VIEW�ɲü�
		// aspectX aspectY �ǿ�ߵı���
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY �ǲü�ͼƬ���
		intent.putExtra("outputX", 200);
		intent.putExtra("outputY", 200);
		intent.putExtra("crop", "true");
		intent.putExtra("scale", true);// ȥ���ڱ�
		intent.putExtra("scaleUpIfNeeded", true);// ȥ���ڱ�
		// intent.putExtra("noFaceDetection", true);//����ʶ��
		intent.putExtra("return-data", true);
		startActivityForResult(intent, 3);

	}

	/**
	 * ��� ��� �ص�����
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// ���
		if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
			String files = SDCARD + pig + ".jpg";
			File file = new File(files);
			if (file.length() > 0) {
				Uri urin = Uri.fromFile(file);
				startPhotoZoom(urin);
			}
		}
		// ���
		else if (requestCode == 2) {
			if (data == null) {
				return;
			}
			startPhotoZoom(data.getData());

		}
		// ��ȡͼƬ
		else if (requestCode == 3) {
			if (data != null) {
				Bundle extras = data.getExtras();
				if (extras != null) {
					Bitmap b = extras.getParcelable("data");
					// ����ͼƬ
					savaBitmapToSd(b, "camera.jpg");
					filen = new File(SDCARD + "camera.jpg");
					photo.setImageBitmap(b);
				}
			}
		}

	}

	/**
	 * �洢bitmap����sd��
	 * 
	 * @param bitmap
	 * @param filename
	 */
	public void savaBitmapToSd(Bitmap bitmap, String filename) {
		if (!android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED)) {
			return;
		}
		File dir = new File(SDCARD);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		filename = SDCARD + filename;
		File f = new File(filename);
		FileOutputStream fOut = null;
		try {
			f.createNewFile();
			fOut = new FileOutputStream(f);
		} catch (Exception e) {
			// e.printStackTrace();
			return;
		}

		try {
			if (bitmap != null && fOut != null) {
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);// ��Bitmap�����������
				fOut.flush();
				fOut.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// ������
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.photo:
			showPhotoDialog();
			break;

		default:
			break;
		}
	}
}
